﻿using Common;
using SLAssetSystem;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ConfigEditorWindow : EditorWindow {
    static ConfigEditorWindow window;
    [MenuItem("CustomTools/Config/ConfigWindow")]
    static void OpenWindow()
    {
        window = GetWindow<ConfigEditorWindow>();
    }

    Vector2 fromPos;
    string fromFilePath="";
    List<string> fileNames = new List<string>();
    
    string toFilePath = "";

    ConfigToCodeUtils.FileType selectIndex = 0;
    string[] fileFilter = new string[] { ".csv", ".xlsx" };

    private void OnEnable()
    {
        fromFilePath = Application.dataPath + "/Resources/" + AssetPaths.ConfigsPath;
        toFilePath = Application.dataPath + "/Scripts/Systems/AssetSystem/Data";

    }

    private void OnGUI()
    {
        GUILayout.BeginVertical();
        string tempFromFilePath = fromFilePath;
        EditorLayoutHelper.FileInput("From:", 50, ref fromFilePath);
        EditorLayoutHelper.FileInput("To:", 50, ref toFilePath);

        GUILayout.BeginHorizontal();
        selectIndex = (ConfigToCodeUtils.FileType)EditorGUILayout.EnumPopup(selectIndex, GUILayout.Width(100));
        if (GUILayout.Button("加载", GUILayout.Width(50)))
        {
            LoadFiles(fromFilePath);
        }
        GUILayout.EndHorizontal();
        fromPos = GUILayout.BeginScrollView(fromPos, GUILayout.MinHeight(window.position.size.y - 100));
        for (int i = 0; i < fileNames.Count; i++)
        {
            GUILayout.Label(fileNames[i]);
        }

        if (GUILayout.Button("转换", GUILayout.Width(50)))
        {
            for (int i = 0; i < fileNames.Count; i++)
            {
                string infos = "";
                string info = "";
                ConfigToCodeUtils.GetCodeByFilePath(selectIndex, fromFilePath+"/", fileNames[i], out infos, out info);
                string infosPath = toFilePath + "/" + AssetPaths.GetFileWithoutExtension(fileNames[i]) + "Infos.cs";
                FileStream infosFile = File.Create(infosPath);
                infosFile.Close();
                File.WriteAllText(infosPath, infos);

                string infoPath = toFilePath + "/" + AssetPaths.GetFileWithoutExtension(fileNames[i]) + "Info.cs";
                FileStream infoFile = File.Create(infoPath);
                infoFile.Close();
                File.WriteAllText(infoPath, info);

            }

        }

        GUILayout.EndScrollView();
        GUILayout.EndVertical();

    }

    private void LoadFiles(string path)
    {
        if (Directory.Exists(path))
        {
            DirectoryInfo info = new DirectoryInfo(path);
            FileInfo[] fileInfos = info.GetFiles();
            fileNames.Clear();
            for (int i = 0; i < fileInfos.Length; i++)
            {
                if (fileInfos[i].Extension == fileFilter[(int)selectIndex])
                {
                    fileNames.Add(fileInfos[i].Name);
                }
            }
        }
        
    }

}
